<?php
/**
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2017/1/5
 * Time: 11:04
 */

namespace Home\Controller;


use Common\Model\GoodsAttrModel;
use Common\Model\GoodsListsModel;
use Common\Model\GoodsModel;
use Common\Model\OrderModel;
use Common\Model\UserAddessModel;
use Think\Controller;

class OrderController extends Controller
{
    public function index(){
        $conf= [
            'js'=>['account','area'],
            'css'=>['account']
        ];
        $orderData  = $_SESSION['order'];
        $this->assign("orderData",$orderData);
        $uid=session('home.uid');
        //dd($uid);die;
        //查询数据库获得已存在的添加地址
        $userAddessModel = new UserAddessModel();
        $oldUserData  = $userAddessModel->where("uid={$uid}")->select();
        //获得用户uid
        $uid = session('home.uid');
        //获取用户的默认地址
        $oldMoRenData = $userAddessModel->where("uid={$uid} AND status=1")->find();
        //分配数据
        $this->assign('oldMoRenData',$oldMoRenData);
        $this->assign('oldUserData',$oldUserData);

        $this->assign('conf',$conf);
        //加载模板
        $this->display();
    }

    /**
     * 购物车提交过来的订单数据
     */
    public  function getCartData(){
        if(IS_POST){
            //检测提交过来是否有订单的sid，有就往下执行
            if(!isset($_POST['caid'])){
                $this->error('请选择订单');die;
            }
            $caids = i('post.caid');
            //这里先将这个数据存入session，方便订单结账后更新购物车
            session('selected_sids',$caids);
            //购物车提交过来的要下单的sid号
            $caids = $_POST['caid'];
            //dd($caid);die;
            //$data= [];
            ////将$sids循环遍历，替换session中的商品的sid
            //foreach($acid as $k=>$v){
            //        $data['order'][$v] = $_SESSION['cart']['goods'][$v];
            //}
            ////重新计算订单总价和订单条数
            //$data['total']=0;
            //$data['total_rows'] = 0;
            //foreach ($data['order'] as $k=>$v){
            //    $data['total'] = $data['total'] + $v['total'];
            //    $data['total_rows'] = $data['total_rows'] + $v['num'];
            //    $data['pic'] = (new GoodsModel())->where("gid={$v['id']}")->getField('pic');
            //}
            //将货品id压入order里
            //获得gtvalue值
            $order = [];
            $user_cart = M('cart');

            foreach($caids as $k=>$v){
                $order['order'][$k] = $user_cart->find($v);
            }
            //将订单列表的总商品数和合计价格压入数组
            $order['total_rows'] = 0;
            $order['total'] = 0;
            foreach($order['order'] as $k=>$v){
                $order['total_rows'] += $v['num'];
                $order['total'] += $v['total'];
            }
            //将数据存入session中，作为中转使用
            session('order',$order);


            //dd($_SESSION);die;
            //dd($_SESSION['order']['gtid']);DIE;
            //跳回order首页
            $this->redirect('Home/Order/index');
        }else{
            //没有就跳回购物页面
            $this->redirect('Home/Cart/index');die;
        }
    }

    //异步添加地址
    public function ajaxAddadess(){
        //将$post数据存入data中
        $data  =$_POST;
        //获得用户的id
        $data['uid'] = session('home.uid');
        //将城市字段变成数组
        $data['city'] = implode(' ',$data['city']);
        //将status改为1
        $data['status'] = 1;
        $userAddessModel = new UserAddessModel();
        //执行添加数据库动作
        //dd($data);die;
        $res=$userAddessModel->store($data);
        //将city赋值给$post
        $_POST['city'] = $data['city'];
        $data['adid'] = $res;
        //如果$res为真就表示添加成功
        if($res['status']=='success'){
            echo json_encode($_POST,JSON_UNESCAPED_UNICODE);die;
        }else{
            echo 0;die;
        }
        //echo(json_encode($data,JSON_UNESCAPED_UNICODE));
    }

    //异步修改收货地址
    public function ajaxEditAddess(){
        //获得用户id
        $uid= session('home.uid');
        //dd($uid);die;
        //获得post提交过来的数据
        $data = $_POST;
        //dd($data);die;
        //获得要编辑的那一条id
        $data['uid'] = session('home.uid');
        //将城市字段拆成数组
        $data['city'] = implode(' ',$_POST['city']);
        //将该条的status状态改为1
        $data['status'] = 1;
        //将数据库其他的状态是改变为0
        $userAddessModel = new UserAddessModel();
        $userAddessModel->where("uid={$uid}")->setField('status',0);
        //执行修改动作
        $res= $userAddessModel->edit($data);
        if($res['status']=='success'){
            echo json_encode($_POST,JSON_UNESCAPED_UNICODE);die;
        }else{
            echo 0;die;

        }
        //echo json_encode($_POST);
    }

}